//
//  IGTopicHeaderCell.m
//  jingdongfang
//
//  Created by BOE on 2017/12/14.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGTopicHeaderCell.h"
#import "IntroViewController.h"

// 顶部个人信息
static NSString *const kTopicHeaderCellID = @"kTopicHeaderCellID";

@interface IGTopicHeaderCell()
@property (nonatomic, strong) UIImageView *headImageView;    //用户头像
@property (nonatomic, strong) UILabel *nameLabel;            //用户名
@property (nonatomic, strong) UILabel *dateLabel;            //创建时间
@property (nonatomic, strong) UIButton *followBtn;           //关注

@end

@implementation IGTopicHeaderCell

+ (IGTopicHeaderCell *)cellWithTableView:(UITableView *)tableView
{
    IGTopicHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:kTopicHeaderCellID];
    if (!cell) {
        cell = [[IGTopicHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kTopicHeaderCellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}

- (void)createUI
{
    CGFloat kAvatarWidth = 30.f;
    __weak typeof(self) weakSelf = self;
    // 发布者头像
    self.headImageView = [[UIImageView alloc] init];
    self.headImageView.backgroundColor = [UIColor mainBackgroundColor];
    self.headImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.contentView addSubview:self.headImageView];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(kGAP15);
        make.width.height.mas_equalTo(kAvatarWidth);
    }];
    self.headImageView.clipsToBounds = YES;
    self.headImageView.userInteractionEnabled = YES;
    self.headImageView.layer.cornerRadius = kAvatarWidth/2;
    [self.headImageView setTapActionWithBlock:^{
        IntroViewController *introVC = [IntroViewController new];
        introVC.u_id = weakSelf.topicModel.userId;
        [UIViewController ig_pushViewController:introVC animated:YES];
    }];
    
    // 发布者姓名
    self.nameLabel = [UILabel new];
    [self.contentView addSubview:self.nameLabel];
    self.nameLabel.textColor = [UIColor c4Color];
    self.nameLabel.preferredMaxLayoutWidth = KSCREENWIDTH - 4 * kGAP15 - kAvatarWidth - 40;
    self.nameLabel.numberOfLines = 1;
    self.nameLabel.font = [UIFont font16];
    self.nameLabel.userInteractionEnabled = YES;
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.headImageView.mas_right).offset(kGAP10);
        make.top.mas_equalTo(self.headImageView);
        make.right.mas_equalTo(-(kGAP15 + kAvatarWidth));
    }];
    [self.nameLabel setTapActionWithBlock:^{
        IntroViewController *introVC = [IntroViewController new];
        introVC.u_id = weakSelf.topicModel.userId;
        [UIViewController ig_pushViewController:introVC animated:YES];
    }];
    
    //时间
    self.dateLabel = [UILabel new];
    self.dateLabel.numberOfLines = 0;
    self.dateLabel.textColor = [UIColor c5Color];
    self.dateLabel.font = [UIFont font12];
    self.dateLabel.preferredMaxLayoutWidth = KSCREENWIDTH - 4 * kGAP15 - kAvatarWidth - 40;
    [self.contentView addSubview:self.dateLabel];
    [self.dateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameLabel.mas_bottom);
        make.left.mas_equalTo(self.headImageView.mas_right).offset(kGAP10);
        make.right.mas_equalTo(-(kGAP15 + kAvatarWidth));
    }];
    
    _followBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_followBtn];
    _followBtn.clipsToBounds=YES;
    _followBtn.layer.borderWidth=1.0f;
    _followBtn.layer.cornerRadius=15.f;
    _followBtn.layer.borderColor=[UIColor c1Color].CGColor;
    _followBtn.contentEdgeInsets=UIEdgeInsetsMake(5, 10, 5, 10);
    _followBtn.titleLabel.font=[UIFont font14];
    [_followBtn setTitleColor:[UIColor c5Color] forState:UIControlStateSelected];
    [_followBtn setTitleColor:[UIColor c1Color] forState:UIControlStateNormal];
    [_followBtn setTitle:NSLocalizedString(@"Followed", nil) forState:UIControlStateSelected];
    [_followBtn setTitle:[NSString stringWithFormat:@"+ %@",NSLocalizedString(@"Follow", nil)] forState:UIControlStateNormal];
    [_followBtn addTarget:self action:@selector(attentionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_followBtn setSelected:NO];
    
    [self.followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(30);
        make.centerY.mas_equalTo(self.nameLabel.mas_centerY);
        make.right.mas_equalTo(-kGAP15);
    }];
}

- (void)setTopicModel:(IGTopicModel *)topicModel {
    _topicModel = topicModel;
    
    self.nameLabel.text = topicModel.userNick;
    self.dateLabel.text = topicModel.publishTime;
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:topicModel.userImg] placeholderImage:[UIImage imageNamed:@"place_head_Icon"]];
}

- (void)attentionBtnClick:(UIButton *)sender{
    sender.selected = !sender.isSelected;
    if ([sender isSelected]) {
        sender.layer.borderColor=[UIColor c5Color].CGColor;
    }else{
        sender.layer.borderColor=[UIColor c1Color].CGColor;
    }
    if (self.focusUser) {
        self.focusUser();
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
